﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Commodity;

namespace FwProject.Manager
{
    public class CommodityReviewsManager : IDomainService
    {
        private readonly IRepository<CommodityReviews, Guid> _commodityReviewsRepository;
        private readonly IRepository<ShopOrderDetails, Guid> _repositoryShopOrderDetails;
        public CommodityReviewsManager(IRepository<CommodityReviews, Guid> commodityReviewsRepository,
            IRepository<ShopOrderDetails, Guid> repositoryShopOrderDetails)
        {
            _commodityReviewsRepository = commodityReviewsRepository;
            _repositoryShopOrderDetails = repositoryShopOrderDetails;
        }
        /// <summary>
        /// 添加一个评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateCommodityReviews(CommodityReviews model)
        {
            var m = _commodityReviewsRepository.GetAll().Where(o => o.Productid == model.Productid && o.CreatorUserId == model.CreatorUserId && o.OrderId==model.OrderId).FirstOrDefault();
            if (m != null)
                throw new UserFriendlyException("此商品您已评论过");

            try
            {
                _repositoryShopOrderDetails.GetAll().Where(o => o.ShopproductId == model.Productid && o.OrderId == model.OrderId).FirstOrDefault().EvaluateStatus = EvaluateStatus.已评价;
            }
            catch (Exception e)
            {
                throw new UserFriendlyException($"此订单商品已删除");

            }
            
            return await _commodityReviewsRepository.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateCommodityReviews(CommodityReviews model)
        {
            await _commodityReviewsRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteCommodityReviews(CommodityReviews model)
        {
            await _commodityReviewsRepository.DeleteAsync(model);
        }
    }
}
